/*
 * Copyright (c) Jan Šlahora (${USER}) - 2011.
 * Created in IntelliJ IDEA 10.5
 */

package MW;

import javax.activation.MimetypesFileTypeMap;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import stuff.FileManager;

/*
*   File MWClasses 
*   in package MW
*   was created by slaha
*   on 16.11.11 at 17:02.
*
*/
public class MWClasses {
  /**
   * Ukončuje program
   * @param status Signál, který ukončovaný program vrací
   */
  public static void konec(int status, MW mw) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("Width", String.valueOf(mw.getWidth()));
    map.put("Height", String.valueOf(mw.getHeight()));
    map.put("Top", String.valueOf(mw.getLocation().getY()));
    map.put("Left", String.valueOf(mw.getLocation().getX()));
    FileManager.writeFile("sett.conf", map);
    System.exit(status);
  }

  public static void otevritSoubor(MW mw) {
    final FileFilter ff = dejFileFilter();

    final JFileChooser fc = new JFileChooser();

    fc.setCurrentDirectory(new File("C:"));

    fc.addChoosableFileFilter(ff);
    fc.setFileFilter(ff);
    int rV = fc.showOpenDialog(mw);

    if (rV == JFileChooser.APPROVE_OPTION) {
      File soubor = fc.getSelectedFile();
      mw.setText(soubor);
    }
  }

  public static void otevritSlozku(MW mw) {

    java.io.FileFilter ff = dejIOFileFilter();
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new java.io.File("."));
    fc.setDialogTitle("Vyberte složku");
    fc.setMultiSelectionEnabled(true);
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fc.setAcceptAllFileFilterUsed(false);

    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
      //File files[] = fc.getSelectedFile().listFiles(ff);
      File files[] = fc.getSelectedFiles();
      for (File f : files) {
        mw.setTexty(f.listFiles(ff));
      }

    }
  }

  private static String dejMime(File f) {
    return new MimetypesFileTypeMap().getContentType(f);
  }

  public static String dejJmeno(String jmeno) {
    String title;
    if (jmeno == null) {
      title = null;
    } else {
      int tecka = jmeno.lastIndexOf('.');
      title = jmeno.substring(0, ((tecka > 0) ? tecka : jmeno.length()));
    }
    return title;
  }

  public static JEditorPane getJEditorPaneOnActualTab(Component c) {
    if (c instanceof JScrollPane) {
      Component[] cc = ((JScrollPane) c).getComponents();
      for (int i = 0; i < cc.length; i++) {
        if (cc[i] instanceof JViewport) {
          Component[] ccc = ((JViewport) cc[i]).getComponents();
          for (int j = 0; j < ccc.length; j++) {
            if (ccc[j] instanceof JEditorPane) {
              return (JEditorPane) ccc[j];
            }
          }
        }
      }
    }
    return null;
  }

  public static void nastavCaretUpdate(final JEditorPane editorPane1) {
    editorPane1.addCaretListener(new CaretListener() {
      @Override
      public void caretUpdate(CaretEvent e) {
        MW mw = (MW) editorPane1.getRootPane().getParent();
        //mw.
      }
    });
  }

  public static void pridejLisenera(final JTabbedPane pane) {
    pane.addChangeListener(new ChangeListener() {
      // This method is called whenever the selected tab changes
      public void stateChanged(ChangeEvent evt) {
        //      pane = (JTabbedPane) evt.getSource();

        // Get current tab

        int sel = pane.getSelectedIndex();
        if (sel < 0) {
          return;
        }

        JRootPane jrp = pane.getRootPane();
        if (jrp!=null) {
          MW mw = (MW) jrp.getParent();
          if ((pane.getTabComponentAt(sel) == mw.dejAktPanel())) {
            return;
          }
          if (mw!=null) {
            JEditorPane jep =getJEditorPaneOnActualTab(pane.getSelectedComponent());
            if (jep!=null) {
              mw.setEditorPane1(jep);
            }
          }
        }
      }
    });
  }

  private static FileFilter dejFileFilter() {
    return new FileFilter() {
      @Override
      public boolean accept(File f) {
        if (f.isDirectory()) {
          return true;
        }

        String jmeno = f.getName();
        String[] rozdeleni = jmeno.split("\\.");
        if (rozdeleni.length == 1) {
          return true;
        }

        String pripona = rozdeleni[rozdeleni.length - 1];
        if (pripona.toLowerCase().equals("txt")) {
          return true;
        }
        return false;
      }

      @Override
      public String getDescription() {
        return "Čistý text (*.txt)";
      }
    };
  }

  private static java.io.FileFilter dejIOFileFilter() {
    return new java.io.FileFilter() {

      @Override
      public boolean accept(File f) {
        if (f.isDirectory()) {
          return true;
        }

        String jmeno = f.getName();
        int pos = jmeno.lastIndexOf(".");
        if (pos == -1) {
          return true;
        }

        
        String pripona = jmeno.substring(pos+1);
        if (pripona.toLowerCase().equals("txt")) {
          return true;
        }
        return false;
      }
    };
  }
}
